From f80f005c602654abe137f152a5128c8abd81f9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20L=2E=20=C5=A0ijanec?= Date: Sun, 3 May 2020 21:00:05 +0200 Subject: variables setting and getting now works from bvr scripts note to future self: don't make this a turing complete language --- a.out | Bin 12252 -> 17000 bytes src/bvr.h | 27 ++++++++++++++++ src/bvrcommands.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/bvrvar.c | 56 ++++++++++++++++++++++++++++++++ src/tape.c | 91 ++++++++++++++++++++++++++++------------------------ test/ram-test.c | 6 ++++ test/tape-test.bvr | 21 +++++++++--- test/tape-test.c | 7 +++- test/var-test.c | 9 ++++++ tmp/output.htm | 31 +++++++++++++++--- 10 files changed, 288 insertions(+), 52 deletions(-) create mode 100644 src/bvr.h create mode 100644 src/bvrcommands.c create mode 100644 src/bvrvar.c create mode 100644 test/ram-test.c create mode 100644 test/var-test.c diff --git a/a.out b/a.out index 8d1341b..c55bdd4 100755 Binary files a/a.out and b/a.out differ diff --git a/src/bvr.h b/src/bvr.h new file mode 100644 index 0000000..c6431ae --- /dev/null +++ b/src/bvr.h @@ -0,0 +1,27 @@ +#pragma once +#include +#define SUCCESS 0 +#define FAILURE -1 +#define COPY_BUFFER_SIZE 128 +#define OPENING_COMMAND_TAG_LENGTH 2 +#define OPENING_COMMAND_TAG_CHAR_1 '<' +#define OPENING_COMMAND_TAG_CHAR_2 '@' +#define CLOSING_COMMAND_TAG_CHAR_1 '@' +#define CLOSING_COMMAND_TAG_CHAR_2 '>' +#define LINE_COMMENT_CHAR '#' +#define LINE_COMMAND_CHAR '?' +#define WAITING_FOR_COMMAND 8922 +#define READING_COMMAND 2343 +#define PROCESSING_COMMAND 346 +#define THE_VOID "/dev/null" + +#define BVR_INITIAL_VARIABLES_COUNT 128 +#define BVR_MAX_VARIABLE_SIZE 128 +#define BVR_UNDEFINED "BVR_UNDEFINED" + +char bvr_variables[BVR_INITIAL_VARIABLES_COUNT*2][BVR_MAX_VARIABLE_SIZE]; +int bvr_bvrvar_first_time_set = 1; + +#define BVR_VER_MAJOR 0 +#define BVR_VER_MINOR 0 +#define BVR_VER_PATCH 0 diff --git a/src/bvrcommands.c b/src/bvrcommands.c new file mode 100644 index 0000000..2e0761b --- /dev/null +++ b/src/bvrcommands.c @@ -0,0 +1,92 @@ +#pragma once +#include +#include +char bvr_var_skip_separator_chars(FILE * input) { + char input_char = fgetc(input); + while(input_char == ' ' || input_char == CLOSING_COMMAND_TAG_CHAR_1 || input_char == ',' || input_char == ';' || input_char == EOF || + input_char == '\0' || input_char == '\n') { + input_char = fgetc(input); + } + return input_char; +} +int bvr_handle_get(FILE * input, FILE * output) { + char item[BVR_MAX_VARIABLE_SIZE+1]; + char input_char = bvr_var_skip_separator_chars(input); + int i = 0; + while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF && + input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) { + item[i++] = input_char; + input_char = fgetc(input); + } + item[i++] = '\0'; + fprintf(output, "%s", bvr_var_get(item)); + fflush(output); + return SUCCESS; +} +int bvr_handle_set(FILE * input, FILE * output) { + char item[BVR_MAX_VARIABLE_SIZE+1]; + char value[BVR_MAX_VARIABLE_SIZE+1]; + char input_char = bvr_var_skip_separator_chars(input); + int i = 0; + while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF && + input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) { + item[i++] = input_char; + input_char = fgetc(input); + } + item[i++] = '\0'; + i = 0; + input_char = bvr_var_skip_separator_chars(input); + while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF && + input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) { + value[i++] = input_char; + input_char = fgetc(input); + } + value[i++] = '\0'; + return bvr_var_set(item, value); + fflush(output); + return SUCCESS; +} +int bvr_handle_include(FILE * input, FILE * output) { + char item[BVR_MAX_VARIABLE_SIZE+1]; + char input_char = bvr_var_skip_separator_chars(input); + int i = 0; + while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF && + input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) { + item[++i] = input_char; + input_char = fgetc(input); + } + item[++i] = '\0'; + fprintf(output, "%s", bvr_var_get(item)); + fflush(output); + return SUCCESS; +} +int bvr_handle_move(FILE * input, FILE * output) { + char item[BVR_MAX_VARIABLE_SIZE+1]; + char value[BVR_MAX_VARIABLE_SIZE+1]; + char input_char = bvr_var_skip_separator_chars(input); + int i = 0; + while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF && + input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) { + item[++i] = input_char; + input_char = fgetc(input); + } + item[++i] = '\0'; + i = 0; + input_char = bvr_var_skip_separator_chars(input); + value[i] = input_char; + while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF && + input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) { + value[++i] = input_char; + input_char = fgetc(input); + } + value[++i] = '\0'; + return bvr_var_mv(item, value); + fflush(output); + return SUCCESS; + +} +int bvr_handle_info(FILE * input, FILE * output) { + // fprintf(stderr, "[bvrcommands.c] bvr_handle_info: bvr bVerbose HTPCMS %d.%d.%d\n", BVR_VER_MAJOR, BVR_VER_MINOR, BVR_VER_PATCH); + fprintf(output, "\nbvr bVerbose HTPCMS %d.%d.%d\n", BVR_VER_MAJOR, BVR_VER_MINOR, BVR_VER_PATCH); + return SUCCESS; +} diff --git a/src/bvrvar.c b/src/bvrvar.c new file mode 100644 index 0000000..97b54b7 --- /dev/null +++ b/src/bvrvar.c @@ -0,0 +1,56 @@ +#pragma once +#include +#include +#include +char * bvr_var_get(char * item) { + for(int i = 0; i < sizeof(bvr_variables)/sizeof(bvr_variables[0]); i=i+2) { + // printf("%s, %s, %d, %d\n", bvr_variables[i], item, sizeof(bvr_variables)/sizeof(bvr_variables[0]), i); + if(strcmp(bvr_variables[i], item) == 0) { + return bvr_variables[i+1]; + } + } + return BVR_UNDEFINED; +} + +int bvr_var_set(char * item, char * value) { + if(bvr_bvrvar_first_time_set == 1) { + for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) { + // printf("loop here1\n"); + strlcpy(bvr_variables[i], BVR_UNDEFINED, sizeof(bvr_variables[i])); + } + bvr_bvrvar_first_time_set = 0; + } + if(strlen(value) >= BVR_MAX_VARIABLE_SIZE) { // >=, ker je še \0, ki ga strlen ne prišteje! + value[BVR_MAX_VARIABLE_SIZE-1] = '\0'; + fprintf(stderr, "[bvrvar.c] bvr_set: value of variable %s too long, chopped to \"%s\"; increase BVR_MAX_VARIABLE_SIZE (%d). Returning FAILURE and setting anyways.\n", + item, value, BVR_MAX_VARIABLE_SIZE); + } + for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) { + // printf("loop here2\n"); + if(strcmp(bvr_variables[i], item) == 0) { + strlcpy(bvr_variables[i+1], value, sizeof(bvr_variables[i+1])); + return SUCCESS; + } + } // could already search for BVR_UNDEFINED here, but idc + for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) { + // printf("loop here4\n"); + if(strcmp(bvr_variables[i], BVR_UNDEFINED) == 0) { + strlcpy(bvr_variables[i], item, sizeof(bvr_variables[i])); + strlcpy(bvr_variables[i+1], value, sizeof(bvr_variables[i+1])); + return SUCCESS; + } + } + fprintf(stderr, "[bvrvar.c] bvr_set: no more space on the variable stack for %s. Increase BVR_INITIAL_VARIABLES_COUNT (%d).", item, BVR_INITIAL_VARIABLES_COUNT); + return FAILURE; +} + +int bvr_var_mv(char * item, char * newname) { + for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) { + if(strcmp(bvr_variables[i], item) == 0) { + strlcpy(bvr_variables[i], newname, sizeof(bvr_variables[i])); + return SUCCESS; + } + } + fprintf(stderr, "[bvrvar.c] bvr_mv: variable %s not found!", item); + return FAILURE; +} diff --git a/src/tape.c b/src/tape.c index f207e8f..5bbec7d 100644 --- a/src/tape.c +++ b/src/tape.c @@ -1,5 +1,5 @@ #pragma once - +#include #include #include #include @@ -8,49 +8,28 @@ #include #include #include -#include -#define BVR_MAX_VARIABLE_SIZE 128 -#define BVR_INITIAL_VARIABLES_COUNT 128 -#define SUCCESS 0 -#define FAILURE -1 -#define COPY_BUFFER_SIZE 128 -#define OPENING_COMMAND_TAG_LENGTH 2 -#define OPENING_COMMAND_TAG_CHAR_1 '<' -#define OPENING_COMMAND_TAG_CHAR_2 '@' -#define CLOSING_COMMAND_TAG_CHAR_1 '@' -#define CLOSING_COMMAND_TAG_CHAR_2 '>' -#define LINE_COMMENT_CHAR '#' -#define LINE_COMMAND_CHAR '?' -#define WAITING_FOR_COMMAND 8922 -#define READING_COMMAND 2343 -#define PROCESSING_COMMAND 346 -char bvr_variables[BVR_INITIAL_VARIABLES_COUNT][BVR_MAX_VARIABLE_SIZE]; -// char* bvr_variables[128] = malloc(BVR_INITIAL_VARIABLES_COUNT * BVR_MAX_VARIABLE_SIZE); -// int bvr_variables_multiplier = 1; - -// int bvr_increase_variables() { // this can be costly https://stackoverflow.com/a/3827922 -// char* more_variables = realloc(bvr_variables, ++bvr_variables_multiplier * BVR_INITIAL_VARIABLES_COUNT * sizeof(BVR_MAX_VARIABLE_SIZE)); -// if (!more_variables) { -// printf("[tape.c] bvr_increase_variables: unable to increase variables count. Program will continue, but may run out of variable space and fail\n"); -// bvr_variables_multiplier = bvr_variables_multiplier - 1; -// return FAILURE; -// } -// return SUCCESS; -// } +#include +#include -int bvr_inline_command_processor(FILE * page_source_file, FILE * temp_output_file, char copy_buffer[]) { +int bvr_inline_command_processor(FILE * page_source_file, FILE * output_file, char copy_buffer[]) { + FILE * temp_output_file = output_file; int what_to_return = SUCCESS; for(int i = 1; i <= OPENING_COMMAND_TAG_LENGTH; i++) { copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // remove opening command tag characters } int command_processor_status = WAITING_FOR_COMMAND; int argument_length = 0; + char command_entered; while(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] != CLOSING_COMMAND_TAG_CHAR_1 || copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] != CLOSING_COMMAND_TAG_CHAR_2 || command_processor_status == PROCESSING_COMMAND) { - copy_buffer[(ftell(page_source_file))% COPY_BUFFER_SIZE] = fgetc(page_source_file); // advance file pointer to the end of command without rewriting. - // printf("received command char %c\n", copy_buffer[(ftell(page_source_file)-2)% COPY_BUFFER_SIZE]); - if(copy_buffer[(ftell(page_source_file)-2)% COPY_BUFFER_SIZE] == '?' && command_processor_status == WAITING_FOR_COMMAND) { + copy_buffer[(ftell(page_source_file))% COPY_BUFFER_SIZE] = fgetc(page_source_file); + if(copy_buffer[(ftell(page_source_file)-2)% COPY_BUFFER_SIZE] == LINE_COMMENT_CHAR && command_processor_status == WAITING_FOR_COMMAND) { + temp_output_file = fopen(THE_VOID, "w"); + continue; + } + if(copy_buffer[(ftell(page_source_file)-2)% COPY_BUFFER_SIZE] == LINE_COMMAND_CHAR && command_processor_status == WAITING_FOR_COMMAND) { + command_entered = copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE]; command_processor_status = READING_COMMAND; continue; } @@ -69,11 +48,37 @@ int bvr_inline_command_processor(FILE * page_source_file, FILE * temp_output_fil argument_string[i] = copy_buffer[0+(ftell(page_source_file)-(argument_length-(1+i)))% COPY_BUFFER_SIZE]; } argument_string[argument_length-2] = '\0'; // -2, ker imamo še CLOSING_COMMAND_TAG_CHAR_1 in CLOSING_COMMAND_TAG_CHAR_2 - printf("end of command, command was %c, argument was \"%s\"\n", copy_buffer[(ftell(page_source_file)-(1+argument_length))% COPY_BUFFER_SIZE], - argument_string); - // switch (copy_buffer[(ftell(page_source_file)-(1+argument_length))]) { // switch command - // - // } + int command_handler_output = SUCCESS; + FILE * argument_feed; + argument_feed = fmemopen (argument_string, strlen (argument_string), "r"); + switch (command_entered) { // switch command + case 'g': + command_handler_output = bvr_handle_get(argument_feed, temp_output_file); + break; + case 's': + command_handler_output = bvr_handle_set(argument_feed, temp_output_file); + break; + case 'i': + command_handler_output = bvr_handle_include(argument_feed, temp_output_file); + break; + case 'm': + command_handler_output = bvr_handle_move(argument_feed, temp_output_file); + break; + case 'b': + // fprintf(stderr, "bunden %c\n", command_entered); + command_handler_output = bvr_handle_info(argument_feed, temp_output_file); + break; + default: + fprintf(stderr, "[tape.c] bvr_inline_command_processor: unknown command %c\n", command_entered); + fprintf(temp_output_file, "\nbVerbose unknown command %c\n", command_entered); + } + if(command_handler_output != SUCCESS) { + fprintf(stderr, "[tape.c] bvr_inline_command_processor: command handler for %c with argument \"%s\" returned an error code.\n", copy_buffer[(ftell(page_source_file)-(1+argument_length))% COPY_BUFFER_SIZE], + argument_string); + fprintf(temp_output_file, "command handler for %c with argument \"%s\" returned an error code.\n", copy_buffer[(ftell(page_source_file)-(1+argument_length))% COPY_BUFFER_SIZE], + argument_string); + return FAILURE; + } return SUCCESS; } } @@ -84,7 +89,7 @@ int bvr_inline_command_processor(FILE * page_source_file, FILE * temp_output_fil return FAILURE; } } - copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // remove closing command tag character + copy_buffer[(ftell(page_source_file)% COPY_BUFFER_SIZE)] = fgetc(page_source_file); // remove closing command tag character return what_to_return; } @@ -98,7 +103,7 @@ int bvr_compose_page(char page_source_file_path[], int this_is_a_top_level_page, FILE * temp_output_file = fopen_mkdir(temp_output_path, "w"); FILE * page_source_file = fopen(page_source_file_path, "r"); - for(int i = 0; i < sizeof(copy_buffer); i++) { // da garbage vrednosti ne bodo slučajno ukazi! + for(int i = 0; i < sizeof(copy_buffer); i++) { // da garbage vrednosti ne bodo slučajno ukazi! --- useless!, todo: delete copy_buffer[i] = ' '; } copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); @@ -107,11 +112,13 @@ int bvr_compose_page(char page_source_file_path[], int this_is_a_top_level_page, } while (1) { copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); - if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == OPENING_COMMAND_TAG_CHAR_1 && copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == OPENING_COMMAND_TAG_CHAR_2) { + if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == OPENING_COMMAND_TAG_CHAR_1 && + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == OPENING_COMMAND_TAG_CHAR_2) { if(bvr_inline_command_processor(page_source_file, temp_output_file, copy_buffer) == FAILURE) { fprintf(temp_output_file, "\nbvr_inline_command_processor returned an error status whilst composing %s\n", page_source_file_path); fprintf(stderr, "[tape.c] bvr_inline_command_processor returned an error status whilst composing %s\n", page_source_file_path); } + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // remove last > that just wants to be there... OB1 continue; } if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == '\n' && copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == LINE_COMMENT_CHAR) { diff --git a/test/ram-test.c b/test/ram-test.c new file mode 100644 index 0000000..8ef0aa2 --- /dev/null +++ b/test/ram-test.c @@ -0,0 +1,6 @@ +#include +int ram_contents[1024]; +int main(int argc, char * argv[]) { + printf("%s\n", ram_contents); + return 0; +} diff --git a/test/tape-test.bvr b/test/tape-test.bvr index c965bc1..f91ceca 100644 --- a/test/tape-test.bvr +++ b/test/tape-test.bvr @@ -1,7 +1,18 @@ -<@?s post_author anton@> +<@?s abc 1232@> +<@?g abc@> +<@?g abc@> +<@?s abc 123@> +<@?g abc@> +<@?g abc@> +<@?s ab 12@> +<@?g ab@> +<@?g ab@> # to je komentar in se ne rendera -<@?i h1@> -<@?s image_caption This is an example image.@> -<@?s example.jpg@> -<@?i image@> +<@?1 post_author@> +<@?2 image_caption This is an example image.@> +<@?3 sexample.jpg@> +<@#?4 image@> +<@?5 image@> +<@?6 image@> +<@?7 image@> in tako so srečno živeli do konca svojih dni! diff --git a/test/tape-test.c b/test/tape-test.c index f1cca25..fc1b2e5 100644 --- a/test/tape-test.c +++ b/test/tape-test.c @@ -6,5 +6,10 @@ extern int main(int argc, char* argv[]) { printf("usage: %s source-file file-with-commands-replaced-with-a's\n", argv[0]); return 1; } - return bvr_compose_page(argv[1], 0, argv[2]); + // bvr_var_set("abc", "1234"); + // bvr_var_set("ab", "123"); + // printf("%s\n", bvr_var_get("abc")); + // printf("%s\n", bvr_var_get("ab")); + bvr_compose_page(argv[1], 0, argv[2]); + return 1; } diff --git a/test/var-test.c b/test/var-test.c new file mode 100644 index 0000000..8c40d85 --- /dev/null +++ b/test/var-test.c @@ -0,0 +1,9 @@ +#include +#include +#include + +int main(int argc, char * argv[]) { + bvr_var_set("abc", "1232"); + printf("%s\n", bvr_var_get("abc")); + return 0; +} diff --git a/tmp/output.htm b/tmp/output.htm index 7dece8a..2dfc960 100644 --- a/tmp/output.htm +++ b/tmp/output.htm @@ -1,5 +1,28 @@ ->> -> -> -> + +1232 +1232 + +123 +123 + +12 +12 +bVerbose unknown command 1 + + +bVerbose unknown command 2 + + +bVerbose unknown command 3 + + + +bVerbose unknown command 5 + + +bVerbose unknown command 6 + + +bVerbose unknown command 7 + in tako so srečno živeli do konca svojih dni! -- cgit v1.2.3